package cn.fl.customer.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCorpRlCompService;
import cn.fl.customer.transform.CstCorpRlCompVOTransform;
import cn.fl.customer.vo.CstCorpRlCompVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.condition.CstCorpRlCompQueryCondition;
import fl.customer.facade.customer.dto.CstCorpRlCompDTO;

/**
 * 客户关联企业关联关系服务实现
 * @author FUJINGRUN
 *
 */
@Service
public class CstCorpRlCompServiceImpl implements ICstCorpRlCompService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CstCorpRlCompServiceImpl.class);
	
	/**
	 * 客户 API服务
	 */
	@Autowired
	private ICustomerApiService customerApiService;
	
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	

	@Override
	public CstCorpRlCompVO selectById(Long id) {
		if(StringUtils.isEmpty(id))return null;
		CstCorpRlCompDTO dto =this.customerApiService.selectCstCorpRlCompById(id);
		this.setValueToCstCorpRlComp(dto);
		return CstCorpRlCompVOTransform.toVO(dto);
	}

	@Override
	public Long add(CstCorpRlCompVO entity) {
		if(null == entity)return null;
		return this.customerApiService.addCstCorpRlCompDTO(CstCorpRlCompVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> idList) {
		if(CollectionUtils.isEmpty(idList))return false;
		return this.customerApiService.removeCstCorpRlCompByIds(idList);
	}

	@Override
	public boolean modify(CstCorpRlCompVO entity) {
		if(null == entity)return false;
		return this.customerApiService.modifyCstCorpRlCompDTO(CstCorpRlCompVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) {
		if(null == page)page = new Pager();
		return this.customerApiService.searchCstCorpRlCompPage(page);
	}
	
	/**
	 * 设置法人关联企业的数据字典项
	 */
	private void setValueToCstCorpRlComp(CstCorpRlCompDTO dto){
		if(dto == null){
			return;
		}
		String rlTypCd = dto.getRlTypCd();
		if(StringUtils.isNotBlank(rlTypCd)){
			SysDictionaryDTO rlTypCdObj = this.sysDictionaryApiService.getCode(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),DictionaryConstant.CODE_GRP_CST_RL_COMP_TYP_CD,rlTypCd);
			if(null !=rlTypCdObj){
				dto.setRlTypCdNm(rlTypCdObj.getCodeNm());
			}
		}
	}
	

	@Override
	public List<KeyValueVO> searchRlTypCdList() {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_RL_COMP_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public List<CstCorpRlCompVO> searchList(CstCorpRlCompQueryCondition qc) {
		if(null==qc) return null;
		return CstCorpRlCompVOTransform.toVOList(customerApiService.searchCstCorpRlCompList(qc));
	}
}
